package com.easylinkin.linkappapi.openapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.service.CommonService;
import com.easylinkin.linkappapi.openapi.mapper.SystemDockDeviceMapper;
import com.easylinkin.linkappapi.openapi.dto.SystemDockDevice;
import com.easylinkin.linkappapi.openapi.service.SystemDockDeviceService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * SystemDockDevice表服务实现类
 *
 * @author CodeGenerator
 * @date 2023/05/12
 */
@Slf4j
@Service("systemDockDeviceService")
public class SystemDockDeviceServiceImpl extends ServiceImpl<SystemDockDeviceMapper, SystemDockDevice>
		implements SystemDockDeviceService {
	@Resource
	private CommonService commonService;
	@Resource
	private LinkappUserContextProducer linkappUserContextProducer;

	@Override
	public boolean saveOne(SystemDockDevice systemDockDevice) {
		commonService.setCreateAndModifyInfo(systemDockDevice);
		// systemDockDevice.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
		validParamRequired(systemDockDevice);
		validRepeat(systemDockDevice);
		validParamFormat(systemDockDevice);
		return save(systemDockDevice);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateOne(SystemDockDevice systemDockDevice) {
		Assert.notNull(systemDockDevice.getId(), "id不能为空");
		commonService.setModifyInfo(systemDockDevice);
		// systemDockDevice.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
		validRepeat(systemDockDevice);
		validParamFormat(systemDockDevice);
		return updateById(systemDockDevice);
	}

	@Override
	public IPage<SystemDockDevice> selectPage(Page page, SystemDockDevice systemDockDevice) {

		return baseMapper.selectPage(page, systemDockDevice);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(List<Long> idList) {
		return removeByIds(idList);
	}

	@Override
	public void export(SystemDockDevice systemDockDevice, HttpServletRequest request, HttpServletResponse response) {
	}

	@Override
	public SystemDockDevice getOneById(Serializable id) {
		return baseMapper.getOneById(id);
	}

	/**
	 * 校验重复
	 */
	private void validRepeat(SystemDockDevice systemDockDevice) {
		/*
		 * QueryWrapper<SystemDockDevice> queryWrapper = new QueryWrapper<>();
		 * queryWrapper.eq("name", systemDockDevice.getName());
		 * queryWrapper.eq("tenant_id",
		 * linkappUserContextProducer.getNotNullCurrent().getTenantId());
		 * List<SystemDockDevice> list = baseMapper.selectList(queryWrapper); if
		 * (list.size() == 0) { return; } if (list.size() > 1) { throw new
		 * BusinessException("名称有重复"); } if
		 * (ObjectUtils.isEmpty(systemDockDevice.getId())) { throw new
		 * BusinessException("名称已存在"); } if
		 * (!systemDockDevice.getId().equals(list.get(0).getId())) { throw new
		 * BusinessException("名称已存在"); }
		 */

	}

	/**
	 * 校验参数必填
	 */
	private void validParamRequired(SystemDockDevice systemDockDevice) {
		// Assert.notNull(systemDockDevice, "参数为空");
		// Assert.isTrue(StringUtils.isNotBlank(systemDockDevice.getName()), "名称为空");
	}

	/**
	 * 校验参数格式
	 */
	private void validParamFormat(SystemDockDevice systemDockDevice) {
		// Assert.isTrue(systemDockDevice.getName() == null ||
		// systemDockDevice.getName().length() <= 50,
		// "名称超长");
	}

	@Override
	public boolean saveAll(List<SystemDockDevice> deviceList) {

		return saveBatch(deviceList);
	}

	/**
	 * 根据关联记录id删除
	 */
	@Override
	public boolean deletedBylinkId(Long linkId) {

		QueryWrapper<SystemDockDevice> qw = new QueryWrapper<>();

		qw.eq("link_id", linkId);

		return retBool(baseMapper.delete(qw));
	}

	@Override
	public boolean deletedBylinkIds(List<Long> idList) {

		QueryWrapper<SystemDockDevice> qw = new QueryWrapper<>();

		qw.in("link_id", idList);

		return retBool(baseMapper.delete(qw));
	}

}
